<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" :value="inpValue" @change="handleChange"/>
    <a-button type="primary" @click="addTask">添加事项</a-button>

    <a-list bordered :dataSource="filterList" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox :checked="item.done" @click="editStatus(item.id)">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="remove(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{unDoneTotal}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="setActiveIndex(0)" :type="activeIndex === 0?'primary':'default'">全部</a-button>
          <a-button @click="setActiveIndex(1)" :type="activeIndex === 1?'primary':'default'">未完成</a-button>
          <a-button @click="setActiveIndex(2)" :type="activeIndex === 2?'primary':'default'">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapActions,mapState,mapMutations,mapGetters} from "vuex";
export default {
  name: "app",
  data() {
    return {
      
    };
  },
  created() {
    this.getList();
  },
  methods: {
    ...mapActions(["getList"]),
    ...mapMutations([
      'addTaskForm',
      'remove',
      'editStatus',
      'clearDone',
      'setActiveIndex'
    ]),
    //输入框的值发生变化时触发
    handleChange(e){
      this.$store.commit('setInpVal',e.target.value);    
    },
    // 添加按钮点击事件
    addTask(){
      if(this.inpValue.trim().length === 0 ) return alert('输入内容不能为空')
      this.addTaskForm()      
    }
  },
  computed: {
    ...mapState([
      'inpValue',
      'activeIndex'
      ]),
    ...mapGetters([
      'unDoneTotal',
      'filterList'
      ])
  }
};
</script>

<style scoped>
#app {
  padding: 10px;
}

.my_ipt {
  width: 500px;
  margin-right: 10px;
}

.dt_list {
  width: 500px;
  margin-top: 10px;
}

.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
